﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using G.Navigate;

namespace G.Navigate
{
    public static class RegiesterState
    {
        /// <summary>
        /// 使用本方法存储数据请确保保存的数据在该页面唯一，否则新的数据将会把原来的覆盖，建议使用SaveToState方法
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="page"></param>
        /// <param name="obj"></param>
        public static void SaveToState<T>(this GBasePage page, T obj) where T : class
        {
            if (page.CanUseState)
                page.State[obj.GetType().FullName + "_Data"] = obj;
        }
        public static void SaveToState(this GBasePage page, string key, object obj)
        {
            if (key == null || key == string.Empty)
            {
                throw new ArgumentException("key不能为空");
            }
            if (page.CanUseState)
                page.State[key + "_Data"] = obj;
        }
        /// <summary>
        /// 使用本方法读取数据请确保保存的数据在该页面唯一
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="page"></param>
        /// <returns></returns>
        public static T ReadFromState<T>(this GBasePage page) where T : class
        {
            if (!page.CanUseState)
                return null;
            if (page.State.ContainsKey(typeof(T).FullName + "_Data"))
            {
                T obj = page.State[typeof(T).FullName + "_Data"] as T;
                return obj;
            }
            else
            {
                return null;
            }
        }
        public static object ReadFromState(this GBasePage page, string key)
        {
            if (key == null || key == string.Empty)
            {
                throw new ArgumentException("key不能为空");
            }

            if (page.CanUseState)
                return page.State.ContainsKey(key + "_Data") ? page.State[key + "_Data"] : null;
            return null;
        }
    }
}
